package com.canyou.activiti;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.activiti.entity.DeploymentVO;
import com.canyou.activiti.entity.ProcessDefinitionDeployDTO;
import com.canyou.activiti.entity.ProcessDefinitionSuspendDTO;
import com.canyou.activiti.entity.ProcessDefinitionVO;
import com.canyou.activiti.service.ProcessDefinitionService;
import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.canyou.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import static com.canyou.api.ResponseBO.ok;

/**
 * <p>
 * 流程部署
 * </p>
 *
 * @author canyou
 * @since 2021-03-02
 */
@RestController
@ApiSort(2001)
@Api(tags = "流程部署")
@RequestMapping("/process-definition")
@Validated
public class ProcessDefinitionController extends BaseApiController {

    @Autowired
    private ProcessDefinitionService processDefinitionService;

    @ApiOperation(value = "分页获取流程部署列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", paramType = "query", value = "当前第几页",
                    dataType = "int", required = false, example = PAGE_INDEX),
            @ApiImplicitParam(name = "pageSize", paramType = "query", value = "每页记录数",
                    dataType = "int", required = false, example = PAGE_SIZE),
            @ApiImplicitParam(name = "processName", paramType = "query", value = "流程部署名称",
                    dataType = "string", required = false, example = ""),
            @ApiImplicitParam(name = "processKey", paramType = "query", value = "流程部署key", dataType = "string")
    })
    @GetMapping("/get_page")
    public ResponseBO<IPage<ProcessDefinitionVO>> getPageList(
            @RequestParam(defaultValue = PAGE_INDEX, required = false) int pageNumber,
            @RequestParam(defaultValue = PAGE_SIZE, required = false) int pageSize,
            @RequestParam(required = false) String processName,
            @RequestParam(required = false) String processKey) {
        IPage<ProcessDefinitionVO> page = new Page<>(pageNumber, pageSize);
        IPage<ProcessDefinitionVO> pageList = processDefinitionService.getPageList(page, processName, processKey);
        return ok(pageList);
    }

    @ApiOperation(value = "删除部署流程")
    @ApiImplicitParam(name = "deploymentId", paramType = "query", value = "部署id",
            dataType = "string", required = true, example = "68cacd2d-7806-11eb-b929-00ff042432dd")
    @GetMapping("/remove")
    public ResponseBO remove(@NotBlank(message = "部署id不能为空") @RequestParam String deploymentId) {
        processDefinitionService.deleteProcessDefinition(deploymentId);
        return ok("删除成功");
    }

    @ApiOperation(value = "激活/挂起部署流程")
    @PostMapping("/active_suspend")
    public ResponseBO activeOrSuspend(
            @ApiParam(name = "processDefinitionSuspendDTO", type = "ProcessDefinitionSuspendDTO", value = "激活/挂起部署流程数据", required = true)
            @Valid
            @NotNull(message = "激活/挂起部署流程信息不能为空")
            @RequestBody ProcessDefinitionSuspendDTO processDefinitionSuspendDTO) {
        processDefinitionService.activeOrSuspendProcessDefinition(processDefinitionSuspendDTO.getProcessDefinitionId(),
                processDefinitionSuspendDTO.getSuspensionState());
        return ok(StringUtils.format("{}成功",
                Integer.valueOf(1).equals(processDefinitionSuspendDTO.getSuspensionState()) ? "激活" : "挂起"));
    }

    @ApiOperation(value = "通过流程图xml字符串添加流程定义")
    @ApiImplicitParam(name = "bpmnStr", paramType = "form", value = "流程图xml字符串",
            dataType = "string", required = true, example = "68cacd2d-7806-11eb-b929-00ff042432dd")
    @PostMapping(value = "/add_deployment_by_string")
    public ResponseBO addDeploymentByString(@NotBlank(message = "流程图xml字符串不能为空")
                                            @RequestParam("stringBPMN") String bpmnStr) {
        Deployment deployment = processDefinitionService.addDeploymentByString(bpmnStr);
        return ok(deployment, "部署成功");
    }

    @ApiOperation(value = "部署流程")
    @PostMapping("/deploy")
    public ResponseBO<DeploymentVO> deployFile(
            @ApiParam(name = "processDefinitionDeployDTO", type = "ProcessDefinitionDeployDTO", value = "上传部署流程文件数据", required = true)
            @Valid
            @NotNull(message = "上传部署流程文件信息不能为空")
            @RequestBody ProcessDefinitionDeployDTO processDefinitionDeployDTO) {
        DeploymentVO deploymentVO = processDefinitionService.deployFile(processDefinitionDeployDTO.getMediaId()
                , processDefinitionDeployDTO.getProcessDefinitionName());
        return ok(deploymentVO, "部署成功");
    }

    @ApiOperation(value = "获取流程定义xml")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deploymentId", paramType = "query", value = "流程部署id",
                    dataType = "string", required = true, example = "68cacd2d-7806-11eb-b929-00ff042432dd"),
            @ApiImplicitParam(name = "resourceName", paramType = "query", value = "流程部署资源名称",
                    dataType = "string", required = true, example = "xxx.bpmn20.xml")
    })
    @GetMapping("/get_xml")
    public void getXml(@RequestParam
                       @NotBlank(message = "部署id不能为空") String deploymentId,
                       @RequestParam
                       @NotBlank(message = "资源名不能为空") String resourceName) {
        processDefinitionService.getXml(response, deploymentId, resourceName);
    }

}
